const debounce = (fn, wait) => {
  let timeout = null;
  return function (...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      fn.apply(this, args);
    }, wait);
  };
};

const throttle = (fn, wait) => {
  let timeout = null;
  return function (...args) {
    if (timeout === null) {
      timeout = setTimeout(() => {
        fn.apply(this, args);
        timeout = null;
      }, wait);
    }
  };
};

const foo = (str) => {
  console.log(new Date().getTime(), str);
};

const scroll = throttle(foo, 1000);
let n = 0;
let s = setInterval(() => {
  n++;
  scroll("hello world");
  if (n > 200) {
    clearInterval(s);
  }
}, 20);
